<template>
	<view class="aplogin-view">
	<!-- 	<view class="vercode-login" @click="cutLogin">
			账号密码登录
		</view> -->

		<view class="logo">
			<image src="@/static/icon/image_60.png" mode="" class="aplogin-image"></image>
		</view>

		<view class="input-all">
			<input type="text" :placeholder="$t('login.emailMobile')" class="mobile-email" v-model="mobileEmail" @focus="hideHint">
			<input type="text" :placeholder="$t('login.code')" class="password" v-model="code" @focus="hideHint">
			<view class="ver-code" @tap="getCodeReg">
				{{ isClick ? countDown : $t("login.getCode") }}
			</view>
		</view>

		<view class="psw">
			<view class="vercode-err" v-if="isVerCode">
				<image src="@/static/icon/image_20.png" class="ver-img" alt=""></image>
				{{ errHint }}
			</view>
		</view>
		
		<view class="psw">
			<view class="vercode-err" v-if="isMobileORemail">
				<image src="@/static/icon/image_20.png" class="ver-img" alt=""></image>
				<view class="">
					{{$t("login.err")}}
				</view>
			</view>
		</view>

		<view class="aplogin-but" @tap="login">
			{{$t("login.login")}}
		</view>

		<view class="login-hint">
			{{$t("tabbar.pointout")}}
			<!-- <navigator url="/pages/register/register" class="go-login">注册</navigator> -->
		</view>

		<!-- <view class="login-list">
			<view class="login-item" @tap="goLogin(item.desc)" v-for="item in loginList" :key="item.imgUrl">
				<image :src="item.imgUrl" class="login-img" alt=""></image>
				{{ item.desc }}
			</view>
		</view> -->
	</view>
</template>

<script>
	import { getEmailCode, checkEmailCode, getUserId, getUserInfo } from "@/common/api.js"
	
	export default {
		data() {
			return {
				isVerCode: false, 
				errHint: "",
				isMobileORemail: false,
				countDown: 60,
				timer: null,
				isClick: false,
				mobileEmail: "",
				code: "",
				loginList: [{
						imgUrl: "../../static/icon/image_15.png",
						desc: "Google" + this.$t("login.login"),
					},
					{
						imgUrl: "../../static/icon/image_17.png",
						desc: this.$t("login.wechat"),
					},
				],
				push_clientid: ""
			}
		},
		methods: {
			async login() {
				const reg = /^\d{11}$/;
				const emailReg = /^[a-zA-Z0-9]+([-_.][A-Za-zd]+)*@([a-zA-Z0-9]+[-.])+[A-Za-zd]{2,5}$/;
				
				const isMobile = reg.test(this.mobileEmail);
				const isEmail = emailReg.test(this.mobileEmail);
				
				if (isMobile) {
					console.log("手机登录");
					checkEmailCode(null, this.mobileEmail, this.code, this.push_clientid)
					.then((res) => {
						console.log(res);
						if(res.code == 200) {
							uni.setStorageSync("token", res.token)
							getUserId()
							.then((res) => {
								if(res.code === 200) {
									// console.log(res.id);
									const userId = res.id;
									uni.setStorageSync("userId", res.id)
									
									getUserInfo()
									.then(res => {
										if(res.code === 200) {
											// console.log(res);
											// const userInfo = res.data;
											uni.setStorageSync("userInfo", res.data)
											uni.switchTab({
												url: "/pages/home/home"
											})
										}
									});
								}
							});
						} else {
							this.errHint = res.msg;
							this.isVerCode = true;
						}
					});
					return
				} else if (isEmail) {
					console.log("邮箱登录");
					checkEmailCode(this.mobileEmail, null, this.code, this.push_clientid)
					.then((res) => {
						console.log(res);
						if(res.code == 200) {
							uni.setStorageSync("token", res.token);
							getUserId()
							.then((res) => {
								if(res.code === 200) {
									// console.log(res.id);
									const userId = res.id;
									uni.setStorageSync("userId", res.id)
									
									getUserInfo()
									.then(res => {
										if(res.code === 200) {
											// console.log(res);
											// const userInfo = res.data;
											uni.setStorageSync("userInfo", res.data)
											uni.switchTab({
												url: "/pages/home/home"
											})
										}
									});
								}
							});
						} else {
							this.errHint = res.msg;
							this.isVerCode = true;
						}
					});
					return
				}
				
				this.isMobileORemail = true;
			},
			hideHint() {
				this.isMobileORemail = false;
				this.isVerCode = false;
			},
			// 获取验证码
			async getCodeReg() {
				const reg = /^\d{11}$/;
				const emailReg = /^[a-zA-Z0-9]+([-_.][A-Za-zd]+)*@([a-zA-Z0-9]+[-.])+[A-Za-zd]{2,5}$/;
				
				const isMobile = reg.test(this.mobileEmail);
				const isEmail = emailReg.test(this.mobileEmail);
				console.log(this.mobileEmail, isEmail, isMobile);
				
				if (isMobile) {
					this.countDownFn();
					console.log("手机验证码");
					getEmailCode(null, this.mobileEmail)
					.then((res) => {
						console.log(res);
						this.code = res.msg;
						this.login();
					});
					return
				} else if (isEmail) {
					this.countDownFn();
					console.log("邮箱验证码");
					getEmailCode(this.mobileEmail, null)
					.then((res) => {
						console.log(res);
						this.code = res.msg;
						this.login();
					});
					return
				}
				
				this.isMobileORemail = true;
			},
			countDownFn() {
				// 正在倒计时
				if (this.isClick) {
					return
				}
				this.isClick = true;
				this.timer = setInterval(() => {
					this.countDown--;
					if (this.countDown <= 0) {
						clearInterval(this.timer);
						this.countDown = 60;
						this.isClick = false;
					}
				}, 1000)	
			},
			cutLogin() {
				this.$emit("cutLogin")
			},
			goLogin(desc) {
				console.log(desc);
				if(desc === "邮箱登录") {
					uni.redirectTo({
						url: "/pages/emailLogin/emailLogin"
					})
				}
			}
		},
		created() {
			// uni-app客户端获取push客户端标记
			uni.getPushClientId({
				success: (res) => {
					this.push_clientid = res.cid
					console.log('客户端推送标识:', this.push_clientid)
				},
				fail(err) {
					console.log(err)
				}
			})
		}
	}
</script>

<style lang="scss" scoped>
	.aplogin-view {
		height: 100%;
		padding: 0 48rpx;
		position: relative;
	}

	.vercode-login {
		color: #FF881D;
		height: 40rpx;
		font-family: PingFang SC;
		font-weight: regular;
		font-size: 30rpx;
		line-height: 40rpx;
		letter-spacing: 0rpx;
		text-align: left;
		position: absolute;
		right: 48rpx;
	}

	.logo {
		text-align: center;

		.aplogin-image {
			margin-top: 149rpx;
			width: 170rpx;
			height: 170rpx;
		}
	}

	.input-all {
		margin-top: 99.6rpx;
		position: relative;

		.mobile-email {
			width: 100%;
			height: 56rpx;
			border-bottom: 2rpx solid #858585;
			padding: 8rpx 0;
		}

		.mobile-email::placeholder {
			color: #858585;
			font-family: PingFang SC;
			font-weight: regular;
			font-size: 30rpx;
			line-height: 40rpx;
			letter-spacing: 0rpx;
			text-align: left;
		}

		.password {
			width: 100%;
			height: 56rpx;
			border-bottom: 2rpx solid #858585;
			margin-top: 48rpx;
			padding: 8rpx 0;
		}

		.password::placeholder {
			color: #858585;
			font-family: PingFang SC;
			font-weight: regular;
			font-size: 30rpx;
			line-height: 40rpx;
			letter-spacing: 0rpx;
			text-align: left;

		}

		.ver-code {
			width: 194rpx;
			height: 56rpx;
			border-radius: 96rpx;
			background: #F1F1F1;
			text-align: center;
			line-height: 56rpx;
			color: #858585;
			font-size: 26rpx;
			position: absolute;
			top: 124rpx;
			right: 0;
		}
	}

	// .psw {
	// 	display: flex;
	// 	align-items: center;
	// 	justify-content: space-between;
	// }

	.vercode-err {
		color: #F44141;
		font-family: PingFang SC;
		font-weight: regular;
		font-size: 26rpx;
		letter-spacing: 0rpx;
		text-align: left;
		margin-top: 12rpx;
		display: flex;
		align-items: center;
	}
	
	.ver-img {
		width: 26rpx;
		height: 26rpx;
		margin-right: 18rpx;
		vertical-align: middle;
	}

	.aplogin-but {
		height: 80rpx;
		width: 100%;
		border-radius: 20rpx;
		background: #000000;
		color: #FFFFFF;
		font-family: PingFang SC;
		font-weight: semibold;
		font-size: 28rpx;
		line-height: 80rpx;
		text-align: center;
		margin-top: 80rpx;
	}

	navigator {
		display: inline;
		font-size: 30rpx;
		color: #ff8f2b;
		border-bottom: 2rpx solid #ff8f2b;
	}

	.login-hint {
		height: 40rpx;
		color: #FF881D;
		font-family: PingFang SC;
		font-weight: regular;
		font-size: 30rpx;
		line-height: 40rpx;
		letter-spacing: 0rpx;
		text-align: center;
		margin-top: 32rpx;
	}

	.login-list {
		margin-top: 56rpx;
	}

	.login-item {
		height: 80rpx;
		border-radius: 20rpx;
		border: 2rpx solid #858585;
		text-align: center;
		color: #000000;
		font-family: PingFang SC;
		font-weight: medium;
		font-size: 24rpx;
		line-height: 80rpx;
		letter-spacing: 0rpx;
		margin-top: 32rpx;
		position: relative;
	}

	.login-img {
		width: 56rpx;
		height: 56rpx;
		position: absolute;
		left: 20rpx;
		top: 12rpx;
	}
</style>
